<ui:composition xmlns="http://www.w3.org/1999/xhtml"
	xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:p="http://primefaces.org/ui"
	xmlns:form="http://java.sun.com/jsf/composite/components/form"
	xmlns:sec="http://www.springframework.org/security/tags"
	template="/templates/include/securedmain.xhtml">

	<ui:define name="content">
		<p:growl id="growl" showDetail="true" sticky="false" autoUpdate="true" />
		<h:form id="form">
			<!-- 		Small workaround as schedule does not initialize backing bean properly -->
			<h:inputHidden value="#{userCalendarBean.id}"></h:inputHidden>
			<sec:authorize ifAnyGranted="ROLE_ADMIN, ROLE_MASTER">
				<h:outputText id="errorToast" styleClass="errorToast"
					value="#{userCalendarBean.errorMessage}"></h:outputText>
				<p:schedule value="#{userCalendarBean.eventModel}"
					widgetVar="myschedule" startWeekday="1" editable="true" locale="cs">

					<p:ajax event="eventMove" update="errorToast"
						listener="#{userCalendarBean.onEventMove}"
						oncomplete="myschedule.update()" />

					<p:ajax event="eventResize" update="errorToast"
						listener="#{userCalendarBean.onEventResize}"
						oncomplete="myschedule.update()" />

					<p:ajax event="eventSelect"
						listener="#{userCalendarBean.onEventSelect}" update=":editDia"
						oncomplete="editDialog.show();" />

					<p:ajax event="dateSelect"
						listener="#{userCalendarBean.onDateSelect}" update=":eventDia"
						oncomplete="eventDialog.show();" />
				</p:schedule>
			</sec:authorize>
			<sec:authorize ifAnyGranted="ROLE_USER">
				<p:schedule value="#{userCalendarBean.eventModel}" draggable="false"
					resizable="false" widgetVar="myschedule" startWeekday="1"
					editable="false">
					<p:ajax event="eventSelect"
						listener="#{userCalendarBean.onEventSelect}" update=":editDia"
						oncomplete="editDialog.show();" />
				</p:schedule>
			</sec:authorize>

		</h:form>


		<p:dialog widgetVar="editDialog"
			header="#{msg['view.actions.calendar.edit']}" id="editDia"
			appendToBody="true" showEffect="clip" hideEffect="clip" modal="true"
			dynamic="true">
			<p:ajax event="close" listener="#{userCalendarBean.resetEditPanel}" />
			<sec:authorize ifAnyGranted="ROLE_ADMIN, ROLE_MASTER">
				<h:form id="actionform">
					<p:commandButton id="deleteButton"
						actionListener="#{userCalendarBean.deleteEvent}"
						disabled="#{!userCalendarBean.event.deletable}"
						value="#{msg['view.actions.delete']}" update="@this"
						icon="ui-icon-trash"
						oncomplete="myschedule.update(); editDialog.hide();" />

					<p:commandButton id="pickUpButton" icon="ui-icon-circle-arrow-n"
						disabled="#{!userCalendarBean.event.pickable}"
						actionListener="#{userCalendarBean.pickUpEvent}"
						value="#{msg['view.actions.button.pickup']}" update="@this"
						oncomplete="myschedule.update(); editDialog.hide();" />

					<p:commandButton id="returnItBackButton"
						disabled="#{!userCalendarBean.event.returnable}"
						icon="ui-icon-circle-arrow-s"
						actionListener="#{userCalendarBean.returnBackEvent}"
						value="#{msg['view.actions.button.returnitback']}" update="@this"
						oncomplete="myschedule.update(); editDialog.hide();" />
				</h:form>
			</sec:authorize>
			<h:form id="editForm">

				<h:inputHidden value="true">
					<f:validator validatorId="reservationValidator" />
					<f:attribute name="since" value="#{sinceEdit}" />
					<f:attribute name="to" value="#{toEdit}" />
				</h:inputHidden>

				<h:panelGrid id="eventEdit" columns="2">

					<h:outputLabel for="usrList" value="#{msg['properties.user']}:" />
					<sec:authorize ifAnyGranted="ROLE_ADMIN, ROLE_MASTER">
						<p:selectOneMenu filter="true" filterMatchMode="startsWith"
							value="#{userCalendarBean.event.user}" effect="fade" var="selusr"
							converter="userConverter" id="usrlist">
							<f:selectItems value="#{userCalendarBean.users}" var="usr"
								itemLabel="#{usr.nickname}" itemValue="#{usr}" id="usrList" />
							<p:ajax event="change" update="@this" />

							<p:column>
								<p:button id="viewButton"
									onclick="window.open('view/viewuser.jsf?id=#{selusr.id}'); return false;"
									title="#{msg['view.actions.view']}" icon="ui-icon-search"
									update="@form" ajax="false">
								</p:button>
							</p:column>
							<p:column>
							#{selusr.nickname}
							</p:column>
						</p:selectOneMenu>



						<h:outputLabel for="perioDisable"
							value="#{msg['properties.period.enabled']}" />
						<p:selectBooleanCheckbox id="perioDisable"
							disabled="#{!userCalendarBean.event.editable}"
							value="#{userCalendarBean.event.enabled}" />
					</sec:authorize>
					<sec:authorize ifAnyGranted="ROLE_USER">
						<h:link id="usrList" outcome="/secured/view/viewuser"
							value="#{userCalendarBean.event.user.nickname}">
							<f:param name="id" value="#{userCalendarBean.event.user.id}" />
						</h:link>
					</sec:authorize>

					<h:outputLabel for="objtext" value="#{msg['properties.object']}:" />
					<h:link id="objtext" outcome="/secured/view/viewobject"
						value="#{userCalendarBean.event.object.name}">
						<f:param name="id" value="#{userCalendarBean.event.object.id}" />
					</h:link>


					<h:outputLabel for="from"
						value="#{msg['properties.period.since']}:" />
					<p:calendar mindate="#{userCalendarBean.currentDate}" id="from"
						value="#{userCalendarBean.event.startDate}" binding="#{sinceEdit}"
						pattern="#{userCalendarBean.dateViewPattern}" disabled="#{!userCalendarBean.event.editable}" />

					<h:outputLabel for="toEdit" value="#{msg['properties.period.to']}:" />
					<p:calendar mindate="#{userCalendarBean.minToDate}" id="toEdit"
						value="#{userCalendarBean.event.endDate}" binding="#{toEdit}" 
						pattern="#{userCalendarBean.dateViewPattern}" disabled="#{!userCalendarBean.event.editable}"/>

					<h:outputLabel for="pickupState"
						value="#{msg['properties.period.state.pickup']}:" />
					<h:outputText id="pickupState"
						value="#{userCalendarBean.event.pickStatus}" />
					<h:outputLabel for="returnItBackState"
						value="#{msg['properties.period.state.returnitback']}:" />
					<h:outputText id="returnItBackState"
						value="#{userCalendarBean.event.returnStatus}" />

					<h:outputLabel for="perioEdit"
						value="#{msg['properties.reservation.periodical']}:" />
					<p:selectBooleanCheckbox id="perioEdit"
						value="#{userCalendarBean.event.periodical}" disabled="true" />

					<h:outputLabel for="weeks"
						rendered="#{userCalendarBean.event.periodical}"
						value="#{msg['view.actions.calendar.howmanyweeks']}:" />
					<p:spinner min="1" max="#{userCalendarBean.maximumWeeks}"
						rendered="#{userCalendarBean.event.periodical}" id="weeksEdit"
						value="#{userCalendarBean.event.numberOfWeeks}" disabled="true" />

				</h:panelGrid>
				<sec:authorize ifAnyGranted="ROLE_ADMIN, ROLE_MASTER">

					<p:commandButton id="editButton"
						disabled="#{!userCalendarBean.event.editable}"
						actionListener="#{userCalendarBean.editEvent}"
						icon="ui-icon-check" value="#{msg['view.actions.button.commit']}"
						update="@this" />
				</sec:authorize>
			</h:form>
		</p:dialog>

		<sec:authorize ifAnyGranted="ROLE_ADMIN, ROLE_MASTER">

			<p:dialog widgetVar="eventDialog"
				header="#{msg['view.actions.calendar.create']}" id="eventDia"
				onHide="myschedule.update()" appendToBody="true" showEffect="clip"
				hideEffect="clip" modal="true" dynamic="true">
							<p:ajax event="close" listener="#{userCalendarBean.resetCreatePanel}" />
				<h:form id="commitForm">

					<h:inputHidden value="true">
						<f:validator validatorId="reservationValidator" />
						<f:attribute name="since" value="#{since}" />
						<f:attribute name="to" value="#{to}" />
					</h:inputHidden>

					<h:panelGrid id="eventDetails" columns="2">

						<h:outputLabel for="objList" value="#{msg['properties.object']}:" />
						<p:selectOneMenu filter="true" filterMatchMode="startsWith"
							value="#{userCalendarBean.event.object}" effect="fade"
							var="selobj" converter="objectConverter">
							<f:selectItems value="#{userCalendarBean.objects}" var="obj"
								itemLabel="#{obj.name}" itemValue="#{obj}" id="objList" />
							<p:ajax event="change" update="@this" />
							<p:column>
								<p:button id="viewButton"
									onclick="window.open('view/viewobject.jsf?id=#{selobj.id}'); return false;"
									title="#{msg['view.actions.view']}" icon="ui-icon-search"
									update="@form" ajax="false">
								</p:button>
							</p:column>
							<p:column>
							#{selobj.name}
							</p:column>
						</p:selectOneMenu>

						<h:outputLabel for="from"
							value="#{msg['properties.period.since']}:" />
						<p:calendar mindate="#{userCalendarBean.currentDate}" id="from"
					
							value="#{userCalendarBean.event.startDate}" binding="#{since}"
							pattern="#{userCalendarBean.dateViewPattern}" />

						<h:outputLabel for="to" value="#{msg['properties.period.to']}:" />
						<p:calendar mindate="#{userCalendarBean.minToDate}" id="to"
							binding="#{to}"
							value="#{userCalendarBean.event.endDate}"
							pattern="#{userCalendarBean.dateViewPattern}" />

						<h:outputLabel for="objList"
							value="#{msg['properties.reservation.periodical']}:" />
						<p:selectBooleanCheckbox
							value="#{userCalendarBean.event.periodical}">
							<p:ajax event="change" update="@this" />
							<p:ajax event="change" update="eventDetails" />
						</p:selectBooleanCheckbox>


						<h:outputLabel for="weeks" id="weeksLabel"
							value="#{msg['view.actions.calendar.howmanyweeks']}:"
							rendered="#{userCalendarBean.event.periodical}" />

						<p:spinner min="1" max="#{userCalendarBean.maximumWeeks}"
							rendered="#{userCalendarBean.event.periodical}" id="weeks"
							value="#{userCalendarBean.event.numberOfWeeks}"
							disabled="#{!userCalendarBean.event.periodical}" />

						<p:commandButton id="commitButton" icon="ui-icon-check"
							actionListener="#{userCalendarBean.addEvent}"
							value="#{msg['view.actions.button.commit']}" update="commitForm @this"
							 />
					</h:panelGrid>
				</h:form>
			</p:dialog>
		</sec:authorize>


	</ui:define>
</ui:composition>
